package com.example.lotterysystem.common.pojo;

import com.example.lotterysystem.common.errorcode.ErrorCode;
import com.example.lotterysystem.common.errorcode.GlobalErrorCodeConstants;
import lombok.Data;
import org.springframework.util.Assert;

import java.io.Serializable;
@Data
public class CommonResult<T>implements Serializable {

    // 返回码
    private Integer code;
    // 返回信息
    private String message;
    // 返回数据
    private T data;

    public static <T>CommonResult<T>success(T data){
        CommonResult<T>result=new CommonResult<>();
result.code= GlobalErrorCodeConstants.SUCCESS.getCode();
        result.data=data;
        result.message="";
        return result;
    }

    public static <T>CommonResult<T>error(Integer code,String message){
        Assert.isTrue(!GlobalErrorCodeConstants.SUCCESS.getCode().equals(code),
                "code 不是错误的异常");
CommonResult<T>result=new CommonResult<>();
        result.code=code;
        result.message=message;
        return result;
    }

    public static <T>CommonResult<T>error(ErrorCode errorCode){
        return error(errorCode.getCode(),errorCode.getMessage());
    }



}
